const state = {
  logList: []
}

const mutations = {
  // 添加一条日志
  addLog (state, payload) {
    state.logList.push(Object.assign(payload, {
      key: new Date().getTime()
    }))
  },
  // 清空日志
  clearLog (state) {
    const len = state.logList.length
    state.logList.splice(0, len)
  }
}

const actions = {
  // 添加一条普通日志
  addLog ({ commit }, log) {
    commit('addLog', {
      log,
      type: 'default'
    })
  },
  // 添加一条警告日志
  addLogWarning ({ commit }, log) {
    commit('addLog', {
      log,
      type: 'warning'
    })
  },
  // 添加一条错误日志
  addLogError ({ commit }, log) {
    commit('addLog', {
      log,
      type: 'error'
    })
  },
  // 添加一条成功日志
  addLogSuccess ({ commit }, log) {
    commit('addLog', {
      log,
      type: 'success'
    })
  },
  // 清空日志
  clearLog ({ commit }) {
    commit('clearLog')
  }
}

const getters = {
  logList (state) {
    return state.logList
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}
